<template>
  <!-- socket.io-client
     1:下载  npm i socket.io-client
     2:导入 import {io} from 'socket.io-client'
     3:使用：
         建立连接
           let ws=io('接口地址')
           ws.addEventListener('connect',()=>{
            // 连接成功
           })
         接收消息
            ws.addEventListener('自行定义',(msg)=>{
                // msg:收到的消息
            })
          发送
            ws.emit('自行定义',发送的内容)
          关闭
            ws.close()
            ws.addEventListener('disconnect',()=>{
                // 关闭了
            })
 -->

  <div>
    <van-button @click="btnClick1">连接</van-button>
    <van-button @click="btnClick2">接收消息</van-button>
    <van-button @click="btnClick3">发送</van-button>
  </div>
</template>
<script>
import { io } from 'socket.io-client'
export default {
  data () {
    return {
      ws: ''
    }
  },

  methods: {
    btnClick1 () {
      this.ws = io('ws://119.91.150.211:1235')
      this.ws.addEventListener('connect', () => {
        console.log('连接成功了')
      })
    },
    btnClick2 () {
      this.ws.addEventListener('message', msg => {
        console.log('收到消息:' + msg)
      })
    },
    btnClick3 () {
      this.ws.emit('message', '123456')
    }
  }
}
</script>
<style></style>
